﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using System.ComponentModel.DataAnnotations.Schema;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Write.API.Applicantions.Command.SaleOrderInfoCommand;
using Yitter.IdGenerator;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 添加开票通知单和物料信息命令处理方法
    /// </summary>
    public class AddInvoiceSettlementEntityCommandHandler : IRequestHandler<AddInvoiceSettlementEntityCommand, APIResult<object>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;
        private readonly IBaseRepository<OrderMaterials> materialBiz;
        private readonly ILogger<AddInvoiceSettlementEntityCommandHandler> logger;

        public AddInvoiceSettlementEntityCommandHandler(IMapper mapper, IBaseRepository<InvoiceSettlementEntity> iRepo, IBaseRepository<OrderMaterials> materialBiz, ILogger<AddInvoiceSettlementEntityCommandHandler> logger)
        {
            this.mapper = mapper;
            this.iRepo = iRepo;
            this.materialBiz = materialBiz;
            this.logger = logger;
        }




        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<object>> Handle(AddInvoiceSettlementEntityCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                InvoiceSettlementEntity enity = new InvoiceSettlementEntity();
                enity.DocumentCode = request.DocumentCode;
                enity.BusinessType = request.BusinessType;
                enity.CustomerName = request.CustomerName;
                enity.Bank = request.Bank;
                enity.DeliveryEmail = request.DeliveryEmail;
                enity.DocumentDate = request.DocumentDate;
                enity.Operator = request.Operator;
                enity.InvoiceTypeFlag = request.InvoiceTypeFlag;
                enity.AccountNumber = request.AccountNumber;
                enity.Remark = request.Remark;
                enity.DocumentStatus = request.DocumentStatus;
                enity.Auditor = request.Auditor;
                enity.TaxRate = request.TaxRate;
                enity.TaxNumber = request.TaxNumber;
                enity.BusinessStatus = request.BusinessStatus;
                enity.AuditDate = request.AuditDate;
                enity.InvoiceType = request.InvoiceType;
                enity.AddressPhone = request.AddressPhone;
                enity.MaterialsCode = YitIdHelper.NextId();
                enity.ShenPiStatus = "待审核";
                int add1 = iRepo.Add(enity).Result;
                if (add1 > 0)
                {
                    request.OrderMaterials.ForEach(x =>
                    {
                        x.SaleCode = enity.DocumentCode;
                    });
                    int add2 = materialBiz.AddRanges(request.OrderMaterials).Result;
                    if (add2 > 0)
                    {
                        result.Code = APIEnum.添加成功;
                        result.Msg = APIEnum.添加成功.ToString();
                    }
                    else
                    {
                        result.Code = APIEnum.添加失败;
                        result.Msg = APIEnum.添加失败.ToString();
                    }
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理请求时发生错误");
                throw;
            }
            return Task.FromResult(result);


        }
       



    }



}
